<template>
  <div id="pro-list">
    <div class="search-box-header">
      <el-select v-model="value" class="m-2" placeholder="全部" size="large">
        <el-option label="全部" value="" />
        <el-option
          v-for="(item, index) in options"
          :key="index"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-input
        v-model="input2"
        class="w-50 m-2"
        placeholder="请输入...."
        size="large"
      />
      <el-button type="primary" @click="goSearch">搜索</el-button>
    </div>
    <el-scrollbar ref="scrollbarRef" height="500px" always>
      <el-table :data="proLists" style="width: 100%">
        <el-table-column type="index" width="70" label="序号" fixed />
        <el-table-column fixed prop="banners" label="图片" width="150">
          <template #default="scope">
            <img :src="scope.row.img1" style="width: 100px; height: 70px" />
          </template>
        </el-table-column>
        <el-table-column fixed prop="proname" label="名称" width="150" />

        <el-table-column prop="category" label="分类" width="120" />
        <el-table-column prop="brand" label="品牌" width="120" />
        <el-table-column prop="originprice" label="价格" width="120" />
        <el-table-column prop="sales" label="销量" width="120" />
        <el-table-column prop="stock" label="库存" width="120" />
        <el-table-column label="是否售卖" fixed="right" width="120">
          <template #default="scope">
            <el-switch v-model="scope.row.issale"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否推荐" fixed="right" width="120">
          <template #default="scope">
            <el-switch
              v-model="scope.row.isrecommend"
              @click="changeIsrecommend(scope.row.proid, scope.row.isrecommend)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否秒杀" fixed="right" width="120">
          <template #default="scope">
            <el-switch
              v-model="scope.row.isseckill"
              @click="changeIsseckill(scope.row.proid, scope.row.isseckill)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template #default>
            <el-button>修改</el-button>
            <el-button>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-scrollbar>
    <div class="demo-pagination-block">
      <div class="demonstration"></div>
      <el-pagination
        v-model:currentPage="pageIndex"
        v-model:page-size="pageCount"
        :page-sizes="[5, 10, 15, 20]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="proList.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script  lang="ts" setup>
import { ref, watchEffect, onMounted, computed } from "vue";
import {
  getProListApi,
  editUpdataFlagApi,
  getCategoryApi,
  getSearchProApi,
} from "@/api/pro";
import { ElMessage } from "element-plus";
import type { protype } from "@/mytype/index";

/* 分页器 */

const small = ref(false);
const background = ref(false);
const disabled = ref(false);
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
};

/* 搜索框 */
const value = ref("");
let options = ref<Array<string>>([]);
/* 搜索 */
function goSearch() {
  getSearchProApi({
    category: value.value,
    search: input2.value,
  }).then((res: any) => {
    if (res.code == "200") {
      proList.value = res.data;
      proList.value.forEach((item) => {
        if (item.isseckill == 1) {
          item.isseckill = true;
        } else {
          item.isseckill = false;
        }
      });
      proList.value.forEach((item) => {
        if (item.isrecommend == 1) {
          item.isrecommend = true;
        } else {
          item.isrecommend = false;
        }
      });
      proList.value.forEach((item) => {
        if (item.issale == 1) {
          item.issale = true;
        } else {
          item.issale = false;
        }
      });

   
    }
  });
}

let proList = ref<Array<protype>>([]);

function getProList() {
  getProListApi().then((res: any) => {
    if (res.code === "200") {
      proList.value = res.data;

      // console.log("proList", proList);
      proList.value.forEach((item) => {
        if (item.isseckill == 1) {
          item.isseckill = true;
        } else {
          item.isseckill = false;
        }
      });
      proList.value.forEach((item) => {
        if (item.isrecommend == 1) {
          item.isrecommend = true;
        } else {
          item.isrecommend = false;
        }
      });
      proList.value.forEach((item) => {
        if (item.issale == 1) {
          item.issale = true;
        } else {
          item.issale = false;
        }
      });
    }
  });
}
const input2 = ref("");
// 每页的条数
let pageCount = ref(5);
// 当前页码
let pageIndex = ref(1);

/* 计算属性，当页码的值发生变化时，重新获取当前页的数据 */
let proLists = computed(() => {
  return proList.value.slice(
    (pageIndex.value - 1) * pageCount.value,
    pageIndex.value * pageCount.value
  );
});

onMounted(() => {
  getProList();
  /* 获取商品分类 */
  getCategoryApi().then((res: any) => {
    if (res.code === "200") {
      options.value = res.data;
    }
  });
});

/* watchEffect(() => {
  // 当页码发生变化时，重新请求数据
  console.log("watchEffect.pageIndex.value", pageIndex.value);
  getProList(pageIndex.value, pageCount.value);
}); */

/* 改变是否推荐 */
function changeIsrecommend(id: string, flag: boolean) {
  console.log("flag", flag);
  // console.log("val", val);

  if (flag === true) {
    editUpdataFlagApi({
      proid: id,
      flag,
      type: "isrecommend",
    }).then((res: any) => {
      console.log(res);
      if (res.code === "200") {
        console.log(res);
        ElMessage({
          message: "该商品已添加到推荐！",
          type: "success",
        });
      } else {
        ElMessage({
          message: "添加失败！",
          type: "error",
        });
      }
    });
  } else {
    editUpdataFlagApi({
      proid: id,
      flag,
      type: "isrecommend",
    }).then((res: any) => {
      console.log(res);
      if (res.code === "200") {
        ElMessage({
          message: "该商品取消推荐成功！",
          type: "success",
        });
      } else {
        ElMessage({
          message: "修改失败！",
          type: "error",
        });
      }
    });
  }
}

/* 改变是否秒杀 */
function changeIsseckill(id: string, flag: boolean) {
  console.log("flag", flag);
  if (flag == true) {
    editUpdataFlagApi({
      proid: id,
      flag,
      type: "isseckill",
    }).then((res: any) => {
      console.log(res);
      if (res.code === "200") {
        ElMessage({
          message: "该商品添加到秒杀！",
          type: "success",
        });
      } else {
        ElMessage({
          message: "修改失败！",
          type: "error",
        });
      }
    });
  } else {
    editUpdataFlagApi({
      proid: id,
      flag,
      type: "isseckill",
    }).then((res: any) => {
      console.log(res);
      if (res.code === "200") {
        ElMessage({
          message: "该商品取消秒杀成功！",
          type: "success",
        });
      } else {
        ElMessage({
          message: "修改失败！",
          type: "error",
        });
      }
    });
  }
}
</script>

<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}

/* 搜索框 */
.w-50 {
  width: 200px;
  margin-left: 15px;
}
.el-button {
  margin-left: 15px;
}
</style>